//
//  HLGudieController.swift
//  weibo-snh
//
//  Created by 黄力 on 15/12/16.
//  Copyright © 2015年 HLTest. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"

class HLGudieController: UICollectionViewController {
    //在创建的时候添加流式布局
    init() {
        //创建布局对象
        let layout = UICollectionViewFlowLayout()
        //设置大小
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //设置布局方向
        layout.scrollDirection = .Horizontal
        //设置cell间的间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        super.init(collectionViewLayout: layout)
        //在初始化完成后设置分页浏览
        collectionView?.pagingEnabled = true
        //隐藏shuip滚动条
        collectionView?.showsHorizontalScrollIndicator = false
        //取消弹簧效果
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(HLGudieCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }


    // MARK: UICollectionViewDataSource


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HLGudieCell
        //赋值
        cell.index = indexPath.item
    
        return cell
    }
    
    //代理方法
    //当cell已经消失的时候调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //获取当前显示的cell
        let gudieCell = collectionView.visibleCells().last as! HLGudieCell
        //获取cell对应的索引
        let cellIndex = collectionView.indexPathForCell(gudieCell)
        //如果当前显示的时最后一个cell,按钮执行动画
        if cellIndex?.item == 3 {
            //设置按钮的比例默认为0
            gudieCell.startBtn.transform = CGAffineTransformMakeScale(0, 0)
            //让按钮执行动画
            gudieCell.startBtnAnimation()
            //添加点击事件
            gudieCell.startBtn.addTarget(self, action: "clickStartBtn", forControlEvents: .TouchUpInside)
        }
    }
    
    //点击开始体验按钮
    @objc private func clickStartBtn() {
        //发送通知改变根控制器
        NSNotificationCenter.defaultCenter().postNotificationName(MainWindowRootControllerChangeNotification, object: nil)
    }
    
}

// MARK:自定义cell

class HLGudieCell: UICollectionViewCell {
    
    //定义索引属性
    var index: Int = 0 {
        didSet {
            //修改图片内容
            backImageView.image = UIImage(named: "new_feature_\(index + 1)")
            
            //隐藏按钮
            startBtn.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //按钮的动画
    func startBtnAnimation() {
        //显示按钮
        startBtn.hidden = false
        //动画
        UIView.animateWithDuration(0.5, delay: 0.1, usingSpringWithDamping: 0.8, initialSpringVelocity: 7, options: [], animations: { () -> Void in
            //将按钮恢复原来大小
            self.startBtn.transform = CGAffineTransformIdentity
            }, completion: nil)
    }
    
    private func setupUI() {
        //添加子控件
        contentView.addSubview(backImageView)
        contentView.addSubview(startBtn)
        //设置自动布局
        //背景图片
        backImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.edges.equalTo(self.snp_edges)
        }
        //按钮
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(backImageView.snp_centerX)
            make.centerY.equalTo(backImageView.snp_bottom).offset(-200)
        }
        
    }
    
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    private lazy var startBtn: UIButton = UIButton(title: "开始体验", titleColor: UIColor.whiteColor(), titleFont: 16, backImageName: "new_feature_finish_button")
  
}

